package com.lzdongrui.backend.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lzdongrui.backend.domain.ApplyLeaveAudit;
import com.lzdongrui.backend.domain.dto.ApplyLeaveDTO;
import com.lzdongrui.backend.domain.vo.ApplyLeaveVO;
import com.lzdongrui.backend.domain.vo.BarChartVO;
import com.lzdongrui.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lzdongrui.common.annotation.Log;
import com.lzdongrui.common.core.controller.BaseController;
import com.lzdongrui.common.core.domain.AjaxResult;
import com.lzdongrui.common.enums.BusinessType;
import com.lzdongrui.backend.domain.ApplyLeave;
import com.lzdongrui.backend.service.IApplyLeaveService;
import com.lzdongrui.common.utils.poi.ExcelUtil;
import com.lzdongrui.common.core.page.TableDataInfo;

/**
 * 请假申请Controller
 * 
 * @author wyh
 * @date 2023-05-16
 */
@RestController
@RequestMapping("/backend/leave")
public class ApplyLeaveController extends BaseController
{
    @Autowired
    private IApplyLeaveService applyLeaveService;

    /**
     * 查询请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApplyLeaveDTO applyLeaveDTO)
    {
        startPage();
        applyLeaveDTO.setUserid(getUserId());
        applyLeaveDTO.setUserDepId(getDeptId());
        List<ApplyLeaveVO> list = applyLeaveService.selectApplyLeaveList(applyLeaveDTO);
        return getDataTable(list);
    }

    /**
     * 导出请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:export')")
    @Log(title = "请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApplyLeaveDTO applyLeaveDTO)
    {
        applyLeaveDTO.setUserid(getUserId());
        applyLeaveDTO.setUserDepId(getDeptId());
        List<ApplyLeaveVO> list = applyLeaveService.selectApplyLeaveList(applyLeaveDTO);
        ExcelUtil<ApplyLeaveVO> util = new ExcelUtil<ApplyLeaveVO>(ApplyLeaveVO.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    /**
     * 获取请假申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(applyLeaveService.selectApplyLeaveById(id));
    }

    /**
     * 新增请假申请
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:add')")
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApplyLeaveDTO applyLeave)
    {
//        applyLeave.setStu(SecurityUtils.getClientUserId());
        return toAjax(applyLeaveService.insertApplyLeave(applyLeave));
    }

    /**
     * 修改请假申请
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:edit')")
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApplyLeave applyLeave)
    {
        return toAjax(applyLeaveService.updateApplyLeave(applyLeave));
    }

    /**
     * 审核请假申请（带记录）
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:audit-leave')")
    @Log(title = "请假审核", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public AjaxResult auditLeave(@RequestBody ApplyLeaveDTO applyLeaveDTO)
    {
        applyLeaveDTO.setAudit(getUserId());
        return toAjax(applyLeaveService.auditLeave(applyLeaveDTO));
    }

    /**
     * 删除请假申请
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:remove')")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(applyLeaveService.deleteApplyLeaveByIds(ids));
    }

    /**
     * 查询延假的历史假期信息
     */
    @PreAuthorize("@ss.hasPermi('backend:leave:old-list')")
    @GetMapping("/old-list")
    public TableDataInfo oldInfoList(ApplyLeaveDTO applyLeaveDTO)
    {
        List<ApplyLeaveVO> list = applyLeaveService.oldInfoList(applyLeaveDTO);
        return getDataTable(list);
    }

    /**
     * 首页柱状图数据
     */
    @GetMapping("/BarChart")
    public TableDataInfo BarChart(ApplyLeaveDTO applyLeaveDTO)
    {
        applyLeaveDTO.setUserid(getUserId());
        applyLeaveDTO.setUserDepId(getDeptId());
        List<BarChartVO> list = applyLeaveService.BarChart(applyLeaveDTO);
        return getDataTable(list);
    }
}
